package com.example.designprinciple.jdbc;

import java.sql.*;

/**
 * @Authors：gaoyang
 * @CreateTime：2021-10-20
 * @Description：todo
 */
public class JDBCConnect {

    public void save(Student student) {
        String sql = "Insert into t_student(name,age)values(?,?)";
        Connection conn = null;
        Statement st = null;
        try {
            //1.加载注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql://jdbcdemo", "root", "root");
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1, student.getName());
            ps.setObject(2, student.getAge());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void delete(Long id){
        String sql="delete from t_student where id=?";
        Connection con = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql:///jdbcdemo","root","root");
            PreparedStatement ps = con.prepareStatement(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
